/*
    Class description : Create a new camera
 */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Komires.MataliPhysics;

namespace MySimpleScene
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SimpleCamera
    {
        Game1 demo;
        PhysicsScene scene;
        string instanceIndexName;

        public SimpleCamera(Game1 demo, int instanceIndex)
        {
            this.demo = demo;
            instanceIndexName = " " + instanceIndex.ToString();
        }

        public void Initialize(PhysicsScene scene)
        {
            this.scene = scene;
        }

        public static void CreateShapes(Game1 demo, PhysicsScene scene)
        {
        }

        public void Create(Vector3 objectPosition, Quaternion objectOrientationX, Quaternion objectOrientationY, Quaternion objectOrientationZ, bool actived)
        {
            Quaternion objectOrientationXY, objectOrientation;
            Matrix rotation;

            PhysicsObject objectRoot = scene.Factory.PhysicsObjectManager.Create("Simple Camera" + instanceIndexName);
            objectRoot.InitLocalTransform.SetPosition(ref objectPosition);
            objectRoot.InitLocalTransform.SetOrientation(objectOrientationX * objectOrientationY * objectOrientationZ);
            objectRoot.CreateCamera(true);
            objectRoot.Camera.Active = actived;
            objectRoot.PostTransformPriority = 1;

            objectRoot.UpdateFromInitLocalTransform();

            Vector3 position = Vector3.Zero;
            Matrix cameraRotation = Matrix.Identity;

            Quaternion.Multiply(ref objectOrientationX, ref objectOrientationY, out objectOrientationXY);
            Quaternion.Multiply(ref objectOrientationXY, ref objectOrientationZ, out objectOrientation);
            Matrix.CreateFromQuaternion(ref objectOrientation, out rotation);

            objectRoot.Camera.SetOrientation(ref objectOrientation);
            objectRoot.Camera.SetRotation(ref rotation);
            objectRoot.Camera.SetEuler(ref rotation);
            objectRoot.Camera.Projection.CreatePerspectiveLH(1.0f, 10000.0f, 70.0f, demo.Window.ClientBounds.Width, demo.Window.ClientBounds.Height);
            objectRoot.MainWorldTransform.GetPosition(ref position);
            objectRoot.Camera.GetTransposeRotation(ref cameraRotation);

            objectRoot.Camera.View.CreateLookAtLH(ref position, ref cameraRotation, 0.0f);
            objectRoot.Camera.UpdateFrustum();

            scene.UpdateFromInitLocalTransform(objectRoot);
        }
    }
}
